package com.qyhl.module_practice.score.mine;

import android.annotation.SuppressLint;

import com.google.gson.Gson;
import com.qyhl.module_practice.common.PracticeUrl;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.UpTokenBean;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeVolunteerDetailBean;

import java.text.SimpleDateFormat;
import java.util.Date;

public class PracticeScoreMineModel implements PracticeScoreMineContract.PracticeScoreMineModel {
    private PracticeScoreMinePresenter mPresenter;

    PracticeScoreMineModel(PracticeScoreMinePresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getDetail(String volId) {

        EasyHttp.get(PracticeUrl.GET_PRACTICE_VOLUNTEER_DETAIL)
                .params("volId", volId)
                .execute(new SimpleCallBack<PracticeVolunteerDetailBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError("暂无任何内容！");
                        } else {
                            mPresenter.setError("请求出错，请稍后再试！");
                        }
                    }

                    @Override
                    public void onSuccess(PracticeVolunteerDetailBean practiceVolunteerDetailBean) {
                        mPresenter.setDetail(practiceVolunteerDetailBean);
                    }
                });
                
    }

    @Override
    public void getToken(boolean isUpload) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CommonUtils.getInstance().getCircleUrl() + "?timestamp=" + time;

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=getUpToken&type=3"))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<UpTokenBean>() {

                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getTokenError(isUpload);
                    }

                    @Override
                    public void onSuccess(UpTokenBean upTokenBean) {
                        mPresenter.getTokenSuccess(upTokenBean, isUpload);
                    }
                });

    }

    @Override
    public void uploadHead(String volId, String logo) {

        EasyHttp.post(PracticeUrl.POST_PRACTICE_HEAD_UPLOAD)
                .params("volId", volId)
                .params("logo", logo)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.uploadError("头像修改失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult  = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.uploadSuccess("头像修改成功！");
                        } else {
                            mPresenter.uploadError("头像修改失败！");
                        }
                    }
                });

    }
}
